package com.nbot.server.workers.impl;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.nbot.common.command.s2b.ReportCommand;
import com.nbot.server.BotServer;

import com.nbot.server.domain.ChecklogDO;
import com.nbot.server.properties.NbotProperties;
import com.nbot.server.service.AlarmpointService;
import com.nbot.server.service.ChecklogService;
import com.nbot.server.workers.OpcWorker;
import org.jinterop.dcom.common.JIException;
import org.openscada.opc.lib.common.ConnectionInformation;
import org.openscada.opc.lib.da.Server;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * opc数据读取类测试用
 */
//@Component("opcWorker")
public class OpcWorkerFade implements OpcWorker {
    private static final Log log = LogFactory.get();

    @Autowired
    private NbotProperties nbotProperties;
    @Autowired
    //private PointService pointService;
    private AlarmpointService alarmpointService;

    @Autowired
    private ChecklogService checklogService;

    @Autowired
    private BotServer botServer;

    private static ScheduledExecutorService pool = Executors.newScheduledThreadPool(50);

    /**
     * 获取server对象
     */

    private Server getOpcServer() {
        ConnectionInformation ci = new ConnectionInformation();
        ci.setHost(nbotProperties.getOpc().getIp());
        /*server端名称，需要更改*/
        ci.setClsid("6031bf75-9cf2-11d1-a97b-00c04fc01389");
//        ci.setClsid(serverList.getClsIdFromProgId(nbotProperties.getOpc().getProgid()));
//6031bf75-9cf2-11d1-a97b-00c04fc01389
        ci.setUser(nbotProperties.getOpc().getUser());
        ci.setPassword(nbotProperties.getOpc().getPassword());

        return new Server(ci, pool);
    }


    @Override
    public String readData(String opcKey) {

        return RandomUtil.randomDouble(1.0, 5.4) + "";

    }

    @Override
    public String checkAllPoint() {
        log.debug("checkAllPoint()");


        //查询报警记录里当前opc点，在10min内是否有相同值的报警，有则不发送报警，无则发送报警
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -Integer.parseInt(nbotProperties.getOpc().getInterval()));//计算n分钟前的date对象
        Date beforeD = beforeTime.getTime();
        List<ChecklogDO> logs = null;
        logs = checklogService.logInNmins("a.a.a", "1", beforeD.getTime() / 1000);
//        List<Checklog> logs = checklogService.selectList(Condition.create()
//                .eq("tag", "a.a.a")
//                .and()
//                .eq("checkValue", "1")
//                .and()
//                .gt("unix_timestamp(checkTimestamp)", beforeD.getTime() / 1000)
//                .orderBy("checkTimestamp", true)
//        );


        //将当前报警值进行保存

        //如果10分钟内没有相同，则发送报警指令
        if (logs.isEmpty()) {

            int priority = 1;
            try {
                priority = Integer.parseInt("1");
            } catch (NumberFormatException e) {
                priority = 1;
            }
            ReportCommand reportCommand = new ReportCommand();
            ((ReportCommand) reportCommand).setPriority(priority);
            // BotServer.putCommand(reportCommand);
            botServer.addCommand(reportCommand);
            log.info("BotServer.comandQueue.size()：" + BotServer.comandQueue.size());
            log.info("发送报警指令：" + reportCommand.toString());


            ChecklogDO checklog = new ChecklogDO();
            checklog.setTag("a.a.a");
            checklog.setChecktimestamp(new Date());
            checklog.setCheckvalue("1");
            checklogService.insert(checklog);


        }


        return null;
    }

    @Override
    public void checkAllAnalogPoint() {

    }

    @Override
    public void checkRelevanceAlarm() throws JIException, UnknownHostException {

    }

    @Override
    public String checkOilWell() throws JIException, UnknownHostException {
        return null;
    }

    @Override
    public String checkWaterWell() throws JIException, UnknownHostException {
        return null;
    }


}
